/*
 * @Author: 刘岩 15136056318@163.com
 * @Date: 2023-11-03 17:25:57
 * @LastEditors: 刘岩 15136056318@163.com
 * @LastEditTime: 2023-11-17 11:57:09
 * @FilePath: /react-template_pc/vite.config.ts
 * @Description:
 */
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
// https://vitejs.dev/config/2
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  return {
    base: './',
    plugins: [react()],
    server: {
      open: false, // 自动在浏览器中打开应用程序
      port: 8888,
      host: '0.0.0.0',
      https: false,
      proxy: {
        '/api': {
          target: env.VITE_APP_BASE_URL,
          changeOrigin: true,
        },
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/assets/styles/px-to-viewport.scss";`,
        },
      },
    },
    resolve: {
      // 别名
      alias: {
        '@': path.resolve(__dirname, 'src'),
        '@scss': path.resolve('src/assets/styles/'),
        '@img': path.resolve('src/assets/img/'),
      },
    },
    build: {
      chunkSizeWarningLimit: 1500, // 调整包的大小
      rollupOptions: {
        output: {
          // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
          entryFileNames: 'assets/js/[name].[hash].js', // 用于命名代码拆分时创建的共享块的输出命名
          chunkFileNames: 'assets/js/[name].[hash].js', // 用于输出静态资源的命名，[ext]表示文件扩展名
          assetFileNames: 'assets/[ext]/[name].[hash].[ext]',
        },
      },
    },
  }
})
